//
//  LNMenuUIView.m
//  iLiteNote
//
//  Created by xiangism on 15/8/28.
//  Copyright (c) 2015年 LiteNice. All rights reserved.
//

#import "LNMenuUIView.h"

@interface LNMenuUIView()
{
    UILabel *_titleView;
    UIControl *_overlayView;
}

@property (nonatomic, strong) UIButton *buttonPull;
@property (nonatomic, strong) UIButton *buttonPush;
@property (nonatomic, strong) UIButton *buttonUnLock;

@end


@implementation LNMenuUIView

- (void)drawRect:(CGRect)rect {
    // Drawing code
}

- (id)initWithFrame:(CGRect)frame andCount:(NSInteger)count {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 1.0f;
        
        self.buttonPull = [UIButton buttonWithType:UIButtonTypeSystem];
        self.buttonPull.frame = CGRectMake(0, 0, TableItemWidth-2, TableItemHeight);
        self.buttonPull.backgroundColor = [UIColor grayColor];
        [self.buttonPull setTitle:@"pull" forState:UIControlStateNormal];
        [self.buttonPull setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [self.buttonPull setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.buttonPull addTarget:self action:@selector(pullTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        int one = TableItemHeight - 23;
        UIImageView *pullImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pull"]];
        pullImg.frame = CGRectMake((TableItemHeight-one)/2, (TableItemHeight-one)/2, one, one);
        [self.buttonPull addSubview:pullImg];
        
        self.buttonPush = [UIButton buttonWithType:UIButtonTypeSystem];
        self.buttonPush.frame = CGRectMake(0, TableItemHeight+1, TableItemWidth-2, TableItemHeight);
        self.buttonPush.backgroundColor = [UIColor grayColor];
        [self.buttonPush setTitle:@"push" forState:UIControlStateNormal];
        [self.buttonPush setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [self.buttonPush setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.buttonPush addTarget:self action:@selector(pushTouch:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *pushImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"push"]];
        pushImg.frame = CGRectMake((TableItemHeight-one)/2, (TableItemHeight-one)/2, one, one);
        [self.buttonPush addSubview:pushImg];
        
        self.buttonUnLock = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.buttonUnLock.frame = CGRectMake(0, TableItemHeight*2+1, TableItemWidth, TableItemHeight);
        self.buttonUnLock.backgroundColor = [UIColor grayColor];
        [self.buttonUnLock setTitle:@"unLock" forState:UIControlStateNormal];
        [self.buttonUnLock setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [self.buttonUnLock setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.buttonUnLock addTarget:self action:@selector(unLockTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.buttonPull];
        [self addSubview:self.buttonPush];
        
        self.backgroundColor = [UIColor grayColor];
        
        _overlayView = [[UIControl alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        _overlayView.backgroundColor = [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:0.0];
        [_overlayView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

#pragma makr - action

- (IBAction)pullTouch:(id)sender {
    if (self.pullBlock) {
        self.pullBlock();
    }
    [self dismiss];
}

- (IBAction)pushTouch:(id)sender {
    if (self.pushBlock) {
        self.pushBlock();
    }
    [self dismiss];
}

- (IBAction)unLockTouch:(id)sender {
    if (self.unLockBlock) {
        self.unLockBlock();
    }
    [self dismiss];
}

#pragma mark - animations

- (void) fadeIn {
    /*CGRect r = self.bounds;
     self.bounds = CGRectMake(r.origin.x, r.origin.y, r.size.width, 0);
     self.center = CGPointMake(r.size.width/2 + _parFrame.origin.x, _parFrame.origin.y);
     [UIView animateWithDuration:0.35 animations:^{
     self.bounds = r;
     self.center = CGPointMake(r.size.width/2 + _parFrame.origin.x, r.size.height/2 + _parFrame.origin.y);
     
     }];*/
    
    //    self.alpha = 0;
    //    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    //    self.transform = CGAffineTransformMakeTranslation(0, -self.bounds.size.height);
    
    //        self.alpha = 1;
    //        self.transform = CGAffineTransformMakeScale(1, 1);
    //        self.transform = CGAffineTransformMakeTranslation(0, 0);
    
    self.alpha = 0;
    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 1;
    }];
}

- (void) fadeOut {
    /*CGRect r = self.bounds;
    [UIView animateWithDuration:0.35 animations: ^{
        self.bounds = CGRectMake(r.origin.x, r.origin.y, r.size.width, 0);
        self.center = CGPointMake(r.size.width/2 + _parFrame.origin.x, _parFrame.origin.y);
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];*/
    [UIView animateWithDuration:0.35 animations:^{
        //self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
    
}

- (void)show {
    UIWindow *keywindow = [[UIApplication sharedApplication]keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    
//    self.center = CGPointMake(keywindow.bounds.size.width/2.0f, keywindow.bounds.size.height/2.0f);
    
    [self fadeIn];
}

- (void)dismiss {
    [self fadeOut];
}

@end
